/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.bridge.major;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import ru.jSkyRoom.bridge.JSkyBridge;
import ru.jSkyRoom.interfaces.major.Armor;
import ru.jSkyRoom.interfaces.major.Texture;
import ru.jSkyRoom.interfaces.stub.JSkyColor;
import ru.jSkyRoom.interfaces.stub.VoiceType;
import ru.jSkyRoom.interfaces.sub.AltTexture;
import ru.jSkyRoom.interfaces.sub.JSkyModel;
import ru.jSkyRoom.interfaces.sub.RaceGenderSettings;
import skyproc.FormID;
import skyproc.Model;
import skyproc.RACE;
import skyproc.SubForm;
import skyproc.genenums.Gender;

/**
 *
 * @author Rakev
 */
public class RaceGenderSettingsBridge extends JSkyBridge<RACE> implements RaceGenderSettings {

    private final Gender gender;

    public RaceGenderSettingsBridge(RACE rawObject, Gender gender) {
        super(rawObject);
        this.gender = gender;
    }

    @Override
    public JSkyModel getModel() {
        return new JSkyModel() {

            @Override
            public Collection<AltTexture> getAltTextures() {
                throw new UnsupportedOperationException("Unsupported operation");
            }

            @Override
            public String getPath() {
                return getRawObject().getModel(gender);
            }

            @Override
            public void setPath(String path) {
                getRawObject().setModel(gender, path);
            }
        };
    }

    @Override
    public VoiceType getVoiceType() {
        FormID id = getRawObject().getVoiceType(gender);
        return getFinderFactory().createVoiceTypeFinder().find(id);
    }

    @Override
    public void setVoiceType(VoiceType voice) {
        FormID id = getIdProducer().produce(voice);
        getRawObject().setVoiceType(gender, id);
    }

    @Override
    public JSkyColor getHairColor() {
        FormID id = getRawObject().getHairColor(gender);
        return getFinderFactory().createJSkyColorFinder().find(id);
    }

    @Override
    public void setHairColor(JSkyColor color) {
        FormID id = getIdProducer().produce(color);
        getRawObject().setHairColor(gender, id);
    }

    @Override
    public Armor getDecapitateArmor() {
        FormID id = getRawObject().getDecapHeadPart(gender);
        return getFinderFactory().createArmorFinder().find(id);
    }

    @Override
    public void setDecapitateArmor(Armor armor) {
        FormID id = getIdProducer().produce(armor);
        getRawObject().setDecapHeadPart(gender, id);
    }

    @Override
    public float getHeight() {
        return getRawObject().getHeight(gender);
    }

    @Override
    public void setHeight(float value) {
        getRawObject().setHeight(gender, value);
    }

    @Override
    public float getWeight() {
        switch (gender) {
            case MALE:
                return getRawObject().getMaleWeight();
            case FEMALE:
                return getRawObject().getFemaleWeight();
        }
        throw new IllegalStateException("Unknown gender: " + gender);
    }

    @Override
    public void setWeight(float value) {
        switch (gender) {
            case MALE:
                getRawObject().setMaleWeight(value);
                return;
            case FEMALE:
                getRawObject().setFemaleWeight(value);
                return;
        }
        throw new IllegalStateException("Unknown gender: " + gender);
    }

    @Override
    public JSkyModel getPhysicsModel() {
        Model model = getRawObject().getPhysicsModel(gender);
        return getBridgeFactory().createBridge(model);
    }

    @Override
    public Texture getDefaultFaceTexture() {
        FormID id = getRawObject().getDefaultFaceTexture(gender);
        return getFinderFactory().createTextureFinder().find(id);
    }

    @Override
    public void setDefaultFaceTexture(Texture texture) {
        FormID id = getIdProducer().produce(texture);
        getRawObject().setDefaultFaceTexture(gender, id);
    }

    @Override
    public Collection<Texture> getFaceDetailsTextureSet() {
        List<FormID> ids = getRawObject().getFaceDetailsTextureSet(gender);
        if (!ids.isEmpty() && ((List) ids).get(0) instanceof SubForm) {
            List<FormID> correctIds = new ArrayList(ids.size());
            for (Object subform : (List) ids) {
                correctIds.add(((SubForm) subform).getForm());
            }
            ids = correctIds;
        }

        Collection<Texture> result = getFinderFactory().createTextureFinder().find(ids);
        return Collections.unmodifiableCollection(result);
    }

    @Override
    public void addFaceDetailsTexture(Texture texture) {
        FormID id = getIdProducer().produce(texture);
        getRawObject().addFaceDetailsTexture(gender, id);
    }

    @Override
    public void removeFaceDetailsTexture(Texture texture) {
        FormID id = getIdProducer().produce(texture);
        getRawObject().removeFaceDetailsTexture(gender, id);
    }

}
